package com.javazyw.utils.security;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class KeyedDigestMD5 {
    
	private static final String DEFAULT_ENCODE = "GBK";
	
	public static byte[] getKeyedDigest(byte[] buffer, byte[] key) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(buffer);
            return md5.digest(key);
        } catch (NoSuchAlgorithmException e) {
        }
        return null;
    }
	
	public static String getKeyedDigest(String strSrc, String key) {
		
		return getKeyedDigest(strSrc, key, DEFAULT_ENCODE);
	}
		
	public static String getKeyedDigest(String strSrc, String key, String encode) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(strSrc.getBytes(encode));
            
            String result="";
            byte[] temp;
            temp=md5.digest(key.getBytes(encode));
    		for (int i=0; i<temp.length; i++){
    			result+=Integer.toHexString((0x000000ff & temp[i]) | 0xffffff00).substring(6);
    		}
    		
    		return result;
    		
        } catch (NoSuchAlgorithmException e) {
        	
        	e.printStackTrace();
        	
        }catch(Exception e)
        {
          e.printStackTrace();
        }
        return null;
    }

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		KeyedDigestMD5 md5=new KeyedDigestMD5();
		String mi;
        String s = "accounttype=private&amount=1.00&bankcardid=4682513d189a7de4c738803a28b6d37f2bec96a85bec3fbd&cardholder=d177e13de77d13050ce3111d35680c54&cardtype=debit&cityid=100100&currency=rmb&mxid=190044&mxorderid=2015032430104732987&notifyurl=http://www.zanshimeiyou.com&paytype=realtime&persisthandling=1&persisttimeout=5&provinceid=100000&reqtime=2015032430104732&securityinfo=1&tradedesc=代付测试啊啊&tradetype=df&version=1.0";
		mi=md5.getKeyedDigest(s,"19pay.com.cn");
		
		System.out.println("mi:"+mi);
		
			
	}

}

